/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.portalnahoubyclient;

import cz.muni.fi.pa165.portalnahouby.api.dto.MushroomDTO;
import java.util.Arrays;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author Jakub Kutil
 */
@UrlBinding("/mushrooms/{$event}/{mushroom.id}")
public class MushroomsActionBean implements ActionBean {

    final static String URI = "http://localhost:8080/pa165/rest/mushrooms";
    private ActionBeanContext context;
    RestTemplate rt = new RestTemplate();
    
    public MushroomsActionBean()
    {
	List<HttpMessageConverter<?>> mc = rt.getMessageConverters();
	HttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
	mc.add( converter );
	rt.setMessageConverters(mc);
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    @DefaultHandler
    public Resolution all() {
        return new ForwardResolution("/mushroom.jsp");
    }

    public List<MushroomDTO> getMushrooms() {
        MushroomDTO[] list = rt.getForObject(URI, MushroomDTO[].class);
        return (Arrays.asList(list));

    }
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "name", required = true, maxlength = 255)
    })
    private MushroomDTO mushroom;

    public MushroomDTO getMushroom() {
        return mushroom;
    }

    public void setMushroom(MushroomDTO mushroom) {
        this.mushroom = mushroom;
    }

    public Resolution add() {
        try {
            rt.put(URI + "/{id}", mushroom, mushroom.getId());

        } catch (HttpServerErrorException ex) {
            return new ForwardResolution("/mushroom_name_duplicity.jsp");
        }
        return new RedirectResolution(this.getClass(), "all");
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadMushroomFromDatabase() {
        String ids = context.getRequest().getParameter("mushroom.id");
        if (ids == null) {
            return;
        }
        mushroom = rt.getForObject(URI + "/{id}", MushroomDTO.class, Long.parseLong(ids));
    }

    public Resolution edit() {
        return new ForwardResolution("/mushroomedit.jsp");
    }

    public Resolution save() {
        try {
            rt.put(URI + "/{id}", mushroom, mushroom.getId());

        } catch (HttpServerErrorException ex) {
            return new ForwardResolution("/mushroom_name_duplicity.jsp");
        }
        return new RedirectResolution(this.getClass(), "all");
    }

	public Resolution delete() {
		try {
			rt.delete(URI + "/{id}", mushroom.getId());

		} catch (HttpServerErrorException ex) {
			return new ForwardResolution("/mushroom_constraint_violation.jsp");
		}
		return new RedirectResolution(this.getClass(), "all");
	}
}